#ifndef SIMPLE_CLIENT_H
#define SIMPLE_CLIENT_H

#include "include/cef_client.h"
#include "include/views/cef_browser_view.h"
#include "include/views/cef_textfield.h"
#include "include/views/cef_window.h"
#include <list>

class SimpleClient : public CefClient,
        public CefContextMenuHandler,
        public CefDisplayHandler,
        public CefDownloadHandler,
        public CefLifeSpanHandler,
        public CefBrowserViewDelegate,
        public CefWindowDelegate
{
public:
    ~SimpleClient();

    static void CreateMainWindow();

private:
    SimpleClient();

    // CefClient methods:
    virtual CefRefPtr<CefContextMenuHandler> GetContextMenuHandler() OVERRIDE {
        return this;
    }
    virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() OVERRIDE {
        return this;
    }
    virtual CefRefPtr<CefDownloadHandler> GetDownloadHandler() OVERRIDE {
        return this;
    }
    virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE {
        return this;
    }

    // CefContextMenuHandler methods:
    virtual void OnBeforeContextMenu(CefRefPtr<CefBrowser> browser,
                                     CefRefPtr<CefFrame> frame,
                                     CefRefPtr<CefContextMenuParams> params,
                                     CefRefPtr<CefMenuModel> model) OVERRIDE;

    // CefDisplayHandler methods:
    virtual void OnAddressChange(CefRefPtr<CefBrowser> browser,
                                 CefRefPtr<CefFrame> frame,
                                 const CefString &url) OVERRIDE;
    virtual void OnTitleChange(CefRefPtr<CefBrowser> browser,
                               const CefString &title) OVERRIDE;
    virtual void OnFaviconURLChange(CefRefPtr<CefBrowser> browser,
                                    const std::vector<CefString> &icon_urls) OVERRIDE;
    virtual bool OnConsoleMessage(CefRefPtr<CefBrowser> browser,
                                  cef_log_severity_t level,
                                  const CefString &message,
                                  const CefString &source,
                                  int line) OVERRIDE;

    // CefDownloadHandler methods:
    virtual void OnBeforeDownload(CefRefPtr<CefBrowser> browser,
                                  CefRefPtr<CefDownloadItem> download_item,
                                  const CefString &suggested_name,
                                  CefRefPtr<CefBeforeDownloadCallback> callback) OVERRIDE;
    virtual void OnDownloadUpdated(CefRefPtr<CefBrowser> browser,
                                   CefRefPtr<CefDownloadItem> download_item,
                                   CefRefPtr<CefDownloadItemCallback> callback) OVERRIDE;

    // CefLifeSpanHandler methods:
    virtual bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE;

    // CefBrowserViewDelegate methods:
    virtual void OnBrowserCreated(CefRefPtr<CefBrowserView> browser_view,
                                  CefRefPtr<CefBrowser> browser) OVERRIDE;
    virtual void OnBrowserDestroyed(CefRefPtr<CefBrowserView> browser_view,
                                    CefRefPtr<CefBrowser> browser) OVERRIDE;
    virtual bool OnPopupBrowserViewCreated(CefRefPtr<CefBrowserView> browser_view,
                                           CefRefPtr<CefBrowserView> popup_browser_view,
                                           bool is_devtools) OVERRIDE;

    // CefWindowDelegate methods:
    virtual bool OnKeyEvent(CefRefPtr<CefWindow> window,
                            const CefKeyEvent &event) OVERRIDE;
    virtual void OnWindowCreated(CefRefPtr<CefWindow> window) OVERRIDE;
    virtual void OnWindowDestroyed(CefRefPtr<CefWindow> window) OVERRIDE;

private:
    void AppendBrowserView(CefRefPtr<CefBrowserView> browser_view);
    void RemoveBrowserView(CefRefPtr<CefBrowserView> browser_view);
    CefRefPtr<CefBrowserView> GetCurrentBrowserView();

    void ShowDefaultPage();

private:
    CefRefPtr<CefWindow> main_window_;
    CefRefPtr<CefTextfield> url_text_field_;
    CefRefPtr<CefPanel> browser_view_panel_;
    std::list<CefRefPtr<CefBrowserView>> browser_view_list_;
    IMPLEMENT_REFCOUNTING(SimpleClient);
};

#endif  // SIMPLE_CLIENT_H
